import {
  Controller,
  Post,
  UploadedFile,
  UseInterceptors,
  Get,
  Res,
} from '@nestjs/common';
import { Response } from 'express';
import { FileInterceptor } from '@nestjs/platform-express';
import { AlbumService } from '../service/album.service';

@Controller('album')
export class AlbumController {
  constructor(private readonly albumService: AlbumService) {}

  //文件上传
  @Post('file')
  @UseInterceptors(FileInterceptor('file')) //获取header的file文件
  upload(@UploadedFile() file) {
    //从UploadedFile可获取
    this.albumService.upload(file);
    return true;
  }

  //文件下载
  @Get('export')
  async downloadAll(@Res() res: Response) {
    //基于流的下载方式
    const { filename, tarStream } = await this.albumService.downloadAll();

    //设置响应to

    res.setHeader('Content-Type', 'application/octet-stream');
    res.setHeader('Content-Disposition', `attachment; filename=${filename}`);
    tarStream.pipe(res); //流转换到res响应
  }
}
